package bbb.ccc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import bbb.BallInfo;
import bbb.BallMap;
import bbb.sss.Utils;

public class RedEncoder {

	public static Long CODE_MAXIMAL = Utils.c(33, 6);

	public static Integer format(BallInfo bi) {
		int ret = 1;
		List<Integer> red = bi.toRedList();
		if (red.get(5) > 6) {
			ret += Utils.c(red.get(5) - 1, 6);
		}

		if (red.get(4) > 5) {
			ret += Utils.c(red.get(4) - 1, 5);
		}

		if (red.get(3) > 4) {
			ret += Utils.c(red.get(3) - 1, 4);
		}

		if (red.get(2) > 3) {
			ret += Utils.c(red.get(2) - 1, 3);
		}

		if (red.get(1) > 2) {
			ret += Utils.c(red.get(1) - 1, 2);
		}

		if (red.get(0) > 1) {
			ret += Utils.c(red.get(0) - 1, 1);
		}

		return ret;
	}

	public static BallInfo parse(Integer code) {
		BallInfo bi = null;
		if (code >= 1 && code <= CODE_MAXIMAL) {
			bi = new BallInfo();

			int[] red = new int[6];
			int temp = code - 1;
			int curIndex = 6;
			int position = 33;
			while (curIndex > 0) {
				if (position > curIndex) {
					long ccc = Utils.c(position - 1, curIndex);
					if (ccc <= temp) {
						temp -= ccc;
						red[curIndex - 1] = position;
						curIndex--;
					}
				} else {
					red[curIndex - 1] = position;
					curIndex--;
				}
				position--;
			}
			bi.setRed1(red[0]);
			bi.setRed2(red[1]);
			bi.setRed3(red[2]);
			bi.setRed4(red[3]);
			bi.setRed5(red[4]);
			bi.setRed6(red[5]);
		}
		return bi;
	}

	public static void main(String[] args) {
		// for (int i = 1; i <= CODE_MAXIMAL; i++) {
		// BallInfo pb1 = parse(i);
		// Integer j = format(pb1);
		// System.out.println(i + ":" + pb1 + ":" + j);
		// }

//		BallMap.loadData("C:\\Users\\Administrator\\Desktop\\双色球\\r.txt");
//		Map<BallInfo, Integer> mm = new HashMap<BallInfo, Integer>();
//		for (BallInfo bi : BallMap.data) {
//			mm.put(bi, format(bi));
//			System.out.println(bi + " ====> " + format(bi));
//		}
//
//		ArrayList<Entry<BallInfo, Integer>> l = new ArrayList<Entry<BallInfo, Integer>>(
//				mm.entrySet());
//		Collections.sort(l, new Comparator<Map.Entry<BallInfo, Integer>>() {
//			public int compare(Map.Entry<BallInfo, Integer> o1,
//					Map.Entry<BallInfo, Integer> o2) {
//				return (o1.getValue() - o2.getValue());
//			}
//		});
//		for (Entry<BallInfo, Integer> e : l) {
//			System.out.println(e.getKey() + " ====> " + e.getValue());
//		}
		System.out.println(CODE_MAXIMAL);
	}
}
